import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Monster implements GameObject {
	private int gravity, ySpeed, xSpeed, x, y, speed, width, height;
	private Image sprite;
	private boolean invert;

	public Monster(int x, int y, String spritePath, int speed) {
		this.x = x;
		this.y = y;
		this.speed = 2;
		ySpeed = 0;
		xSpeed = this.speed;
		gravity = 0;
		invert = false;

		ImageIcon ii = new ImageIcon(this.getClass().getResource(spritePath));
		sprite = ii.getImage();

		width = sprite.getWidth(null);
		height = sprite.getHeight(null);
	}

	/*
	 * public void gravity(ArrayList objects) { if (gravity > 0) { y += gravity;
	 * if (!collision(objects)) { y -= gravity; } } }
	 */

	public int getX() {
		return x;
	}

	public void setXSpeed(int xSpeed) {
		this.xSpeed = xSpeed;
	}

	public int getY() {
		return y;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public void move(ArrayList objects) {
		x += xSpeed;
		while (collision(objects)) {
			if (xSpeed < 0)
				x += 1;
			else
				x -= 1;
			invert = true;
		}
		if (invert) {
			xSpeed = -xSpeed;
			invert = false;
		}
		y += ySpeed + gravity;
		while (collision(objects)) {
			if (ySpeed < 0)
				y += 1;
			else
				y -= 1;
		}
	}

	public void draw(Graphics2D g2d, Game observer) {
		g2d.drawImage(sprite, x, y, observer);
	}

	public boolean collision(ArrayList objects) {
		Rectangle rM = new Rectangle(getX(), getY(), getWidth(), getHeight());
		for (int i = 0; i < objects.size(); i++) {
			Block b = (Block) objects.get(i);
			Rectangle rB = new Rectangle(b.getX(), b.getY(), b.getWidth(), b.getHeight());

			if (rM.intersects(rB)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public int getType() {
		if (xSpeed != 0 || ySpeed !=0)
			return 2;
		else
			return 1;
	}

	@Override
	public int getYSpeed() {
		return ySpeed;
	}

	@Override
	public int getXSpeed() {
		return xSpeed;
	}

	@Override
	public void specialFunction(int type) {
	}

	public int projectileCollision(int type) {
		if(type == 0){
			xSpeed = 0;
		}
		else if(type == 1){
			xSpeed = speed;
		}
		
		return -1;
	}
}
